package com.renjianbo.project.module.system.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;

import com.renjianbo.project.common.model.LoginUser;
import com.renjianbo.project.common.model.PageInfo;
import com.renjianbo.project.common.util.JsonUtil;
import com.renjianbo.project.common.model.Rest;
import com.renjianbo.project.common.model.PageReq;
import com.renjianbo.project.module.system.model.sys_user.*;
import com.renjianbo.project.module.system.service.SysUserService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.BeanUtils;
import java.util.List;

import org.springframework.web.bind.annotation.RestController;

/**
 * <p>
 * 系统-用户
 * </p>
 *
 * @author 任建波
 * @since 2022-05-20
 */
@Api(tags = {"系统-用户"})
@RestController
@RequestMapping("sys-user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @ApiOperation(value = "登录")
    @PostMapping("login")
    public Rest<String> login(@RequestBody @Validated SysUserLoginReq req){
        String token = sysUserService.login(req);

        return Rest.success(token);
    }

    @ApiOperation(value = "用户详情")
    @GetMapping("user-info")
    public Rest<LoginUser> userInfo(){
        LoginUser loginUser = sysUserService.userInfo();

        return Rest.success(loginUser);
    }

    @ApiOperation(value = "查询系统-用户分页数据")
    @GetMapping("page")
    public Rest<PageInfo<SysUserVO>> page(SysUserSearchReq req, PageReq pageReq){
        IPage<SysUserDTO> iPage = sysUserService.page(req, pageReq);
        PageInfo<SysUserVO> pageInfo = JsonUtil.iPageTransform(iPage, SysUserVO.class);

        return Rest.success(pageInfo);
    }

    @ApiOperation(value = "新增系统-用户")
    @PostMapping("add")
    public Rest<String> add(@RequestBody @Validated SysUserAddReq req){
        sysUserService.add(req);

        return Rest.success();
    }

    @ApiOperation(value = "删除系统-用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="系统-用户-id",required=true),
    })
    @PostMapping("delete")
    public Rest<String> delete(@RequestParam Long id){
        sysUserService.delete(id);

        return Rest.success();
    }

    @ApiOperation(value = "更新系统-用户")
    @PostMapping("update")
    public Rest<String> update(@RequestBody @Validated SysUserUpdateReq req){
        sysUserService.update(req);

        return Rest.success();
    }

    @ApiOperation(value = "根据id查询系统-用户")
    @ApiImplicitParams({
        @ApiImplicitParam(name="id",value="系统-用户-id",required=true),
    })
    @GetMapping("get-by-id")
    public Rest<SysUserVO> getById(@RequestParam Long id){
        SysUserDTO sysUserDTO = sysUserService.getById(id);
        SysUserVO sysUserVO = new SysUserVO();
        BeanUtils.copyProperties(sysUserDTO, sysUserVO);

        return Rest.success(sysUserVO);
    }

}
